#include"Util.hpp"
#include<iostream>
#include<vector>
#include<string>
#include<chrono>
#include<unordered_map>
#include<sw/redis++/redis++.h>
#include<thread>
using namespace std;
using namespace std::chrono_literals;

//get 和 set
void test1(sw::redis::Redis& redis)
{
    cout << "get和set的使用" << endl;
    //清空数据库
    redis.flushall();
    //使用set设置key
    redis.set("key1","111");
    redis.set("key2","222");
    redis.set("key3","333");
    //使用get获取
    auto val1 = redis.get("key1");
    if(val1)
    cout << "value1=" << val1.value() << endl;
    auto val2 = redis.get("key2");
    if(val2)
    cout << "value2=" << val2.value() << endl;
    auto val3 = redis.get("key3");
    if(val3)
    cout << "value3=" << val3.value() << endl;
    auto val4 = redis.get("key4");
    if(val4)
    cout << "value4=" << val4.value() << endl; //nil无效值
     
}

void test2(sw::redis::Redis& redis)
{
     cout << "exists的使用" << endl;
     redis.flushall();
     redis.set("key1","val1");
     redis.set("key2","val2");
     auto ret = redis.exists({"key1","key2","key3"});
     cout << ret << endl;
    
}

void test3(sw::redis::Redis& redis)
{
     cout << "del的使用" << endl;
     redis.flushall();
     redis.set("key1","val1");
     redis.set("key2","val2");
     redis.set("key3","val3");
     //一次删除一个
     auto ret1 = redis.del("key1");
     if(redis.exists("key1") == 0) cout << "key1被成功删除.." << endl;
     //一次删除多个
     auto ret2 = redis.del({"key2","key3","key4"});
     cout << ret2 << endl;
}



void test4(sw::redis::Redis& redis)
{
     cout << "keys的使用" << endl;
     redis.flushall();
     redis.set("key1","val1");
     redis.set("key2","val2");
     redis.set("key3","val3");
     redis.set("key4","val4");
     redis.set("key5","val5");
     //
     vector<string> result;
     auto it = std::back_inserter(result); //插入迭代器：插入到末尾
     redis.keys("*",it);
     printContainer(result);
}

void test5(sw::redis::Redis& redis)
{
    cout << "expire的使用" << endl;
    redis.flushall();
    redis.set("key1","111");
    redis.expire("key1",10s);//这种写法不清楚10是什么含义
    redis.set("key2","222");
    redis.expire("key2",std::chrono::seconds(20));

    std::this_thread::sleep_for(3s); //3s是C++11之后扩展的字面值常量，带上s后缀代表是second类型
    auto time1 = redis.ttl("key1");
    auto time2 = redis.ttl("key2");
    cout << "time1: " << time1 << endl;
    cout << "time2: " << time2 << endl;
}

void test6(sw::redis::Redis& redis)
{
     cout << "type的使用" << endl;
     redis.flushall();
     redis.set("key1","111");
     string ret1 = redis.type("key1");
     cout << "key1 type: "<< ret1 << endl;
     //列表类型
     redis.lpush("key2","111");
     string ret2 = redis.type("key2");
     cout << "key2 type: "<< ret2 << endl;
     //hash类型
     redis.hset("key3","aaa","111");
     string ret3 = redis.type("key3");
     cout << "key3 type: "<< ret3 << endl;
     //set
     redis.sadd("key4","aaa");
     string ret4 = redis.type("key4");
     cout << "key4 type: "<< ret4 << endl;
     //zset
     redis.zadd("key5","mike",88);
     string ret5 = redis.type("key5");
     cout << "key5 type: "<< ret5 << endl;
}



int main()
{
    //创建Redis对象时需要在构造函数中指定redis服务器的端口和地址
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    test6(redis);
    //调用ping方法让客户端给服务器发ping，服务器会返回pong
    // string ret = redis.ping();
    // cout << ret << endl;
    // return 0;
}
